﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using LHS.Core.TableInfo;
using MS.Core.Constants.Objects;
using System.Collections;

namespace LHS.Win
{
    public partial class frmNotToaThuoc : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmNotToaThuoc()
        {
            this.InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            if (this.rbAll.Checked)
            {
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONDATE, " between '" + this.dtpDate.Value.ToString("MM/dd/yyyy") + " 00:00:00 AM' and dateAdd(dd,1,'" + this.dtpDate.Value.ToString("MM/dd/yyyy") + " 00:00:00 AM') ", ExpressionObject.eSelectOperator.None));
                DataTable dt = objController.GetData("vw_SetUpPrescription", param);
             
                if (dt == null || dt.Rows.Count == 0)
                {
                    this.dataGrid1.DataSource = null;
                    this.dtpDate.Enabled = true;
                    MessageBox.Show("Không có bệnh nhân nào chưa cấp toa thuốc vào ngày này!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.dataGrid1.Enabled = true;
                    bindingSource1.DataSource = dt;
                    this.dataGrid1.DataSource = bindingSource1;
                    this.dataGrid1.ReadOnly = true;
                    this.dataGrid1.TableStyles.Clear();
                    this.loadDataGrid();
                }
            }
            else if (this.tbKeySearch.Text == "")
            {
                MessageBox.Show("Vui lòng nhập từ khóa để tìm kiếm!", "Lỗi tìm kiếm", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                List<ExpressionObject> param = new List<ExpressionObject>();
                if (this.rbID.Checked)
                {
                    param.Add(new ExpressionObject(InfoPatients.PATID,this.tbKeySearch.Text));
                }
                else
                {
                    param.Add(new ExpressionObject(InfoPatients.PATNAME, this.tbKeySearch.Text, ExpressionObject.eSelectOperator.LikeAll));
                }
               DataTable dt= objController.GetData("vw_SetUpPrescription",param);
              
                if (dt == null || dt.Rows.Count == 0)
                {
                    MessageBox.Show("Không tìm thấy bệnh nhân: " + this.tbKeySearch.Text.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    bindingSource1.DataSource = dt;
                    this.dataGrid1.DataSource = bindingSource1;
                    this.dataGrid1.ReadOnly = true;
                    this.dataGrid1.TableStyles.Clear();
                    this.loadDataGrid();
                }
            }
        }

        private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                this.dataGrid1.Select(this.dataGrid1.CurrentRowIndex);
                if (MessageBox.Show("Bạn có muốn lập toa thuốc cho bệnh nhân này không ?", "Lập Toa Thuốc", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.clrISN = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 0].ToString();
                    this.patName = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 2].ToString();
                    this.patID = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 1].ToString();
                    string str = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 5].ToString();
                    string str2 = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 6].ToString();

                    //phan them moi --edit by thaoht 20090531
                    DataTable dt = (DataTable)bindingSource1.DataSource;
                    DataRow dr = dt.Rows[this.dataGrid1.CurrentRowIndex];

                    byte[] byteEmpSignature = null; //edit 20090604 --phan chữ ký điện tử
                    if (dr[InfoEmployees.EMPSIGNATUREIMAGE] is byte[])
                        byteEmpSignature = (byte[])dr[InfoEmployees.EMPSIGNATUREIMAGE];

                    Hashtable _hashParam = new Hashtable();
                    _hashParam[InfoEmployees.EMPID] = dr[InfoEmployees.EMPID].ToString();
                    _hashParam[InfoEmployees.EMPNAME] = dr[InfoEmployees.EMPNAME].ToString();
                    _hashParam[InfoPatients.PATID] = dr[InfoPatients.PATID].ToString();
                    _hashParam[InfoPatients.PATNAME] = dr[InfoPatients.PATNAME].ToString();
                    _hashParam[InfoPatients.PATSEX] = dr[InfoPatients.PATSEX].ToString();
                    _hashParam[InfoPatients.PATAGE] = dr[InfoPatients.PATAGE].ToString();
                    _hashParam[InfoPatients.PATADDRESS] = dr[InfoPatients.PATADDRESS].ToString();
                    _hashParam[InfoClinicalRecord.CLRMEDICALADVICE] = dr[InfoClinicalRecord.CLRMEDICALADVICE].ToString();
                    _hashParam[InfoClinicalRecord.CLRSKILL] = dr[InfoClinicalRecord.CLRSKILL].ToString();
                    _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] = dr[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].ToString();
                    _hashParam[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE] = dr[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE].ToString();
                    //end phan them moi --edit by thaoht 20090531

                    frmToaThuocHoSoBenhAn an = new frmToaThuocHoSoBenhAn(this.clrISN, this.patID, this.patName, this.clrISN, _hashParam, byteEmpSignature);
                    an.ShowInTaskbar = false;
                    an.ShowDialog(this);
                    this.frmBenhAnChuaTTDuTien_Load(sender, e);
                }
            }
        }
        private void frmBenhAnChuaTTDuTien_Load(object sender, EventArgs e)
        {
            this.dtpDate.Value = DateTime.Now;
            List<ExpressionObject> param = new List<ExpressionObject>();
            param.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONDATE, " between '" + this.dtpDate.Value.ToString("MM/dd/yyyy") + " 00:00:00 AM' and dateAdd(dd,1,'" + this.dtpDate.Value.ToString("MM/dd/yyyy") + " 00:00:00 AM')", ExpressionObject.eSelectOperator.None));
            param.Add(new ExpressionObject(InfoEmployees.EMPNAME," is not null", ExpressionObject.eSelectOperator.None));
            DataTable dt = objController.GetData("vw_SetUpPrescription", param);
           
            if (dt == null || dt.Rows.Count == 0)
            {
                this.dataGrid1.DataSource = null;
            }
            else
            {
                this.dataGrid1.Enabled = true; 
                bindingSource1.DataSource = dt;
                this.dataGrid1.DataSource = bindingSource1;
                this.dataGrid1.ReadOnly = true;
                this.dataGrid1.TableStyles.Clear();
                this.loadDataGrid();
            }
        }

        public void loadDataGrid()
        {
            DataGridTableStyle table = new DataGridTableStyle();
            table.MappingName = "vw_SetUpPrescription";
            table.AlternatingBackColor = Color.AliceBlue;
            table.HeaderForeColor = Color.Maroon;
            table.GridLineColor = Color.CornflowerBlue;

            DataGridTextBoxColumn column = new DataGridTextBoxColumn();
            column.MappingName = InfoClinicalRecord.CLRISN;
            column.HeaderText = "";
            column.Width = 0;
            table.GridColumnStyles.Add(column);

            DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
            column2.MappingName = InfoPatients.PATID;
            column2.HeaderText = "Số ID";
            column2.Width = 100;
            table.GridColumnStyles.Add(column2);

            DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();
            column3.MappingName = InfoPatients.PATNAME;
            column3.HeaderText = "Tên Bệnh Nhân";
            column3.Width = 210;
            table.GridColumnStyles.Add(column3);

            DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();
            column4.MappingName = InfoPatients.PATBIRTHDATE;
            column4.HeaderText = "Ngày Sinh";
            column4.Width = 100;
            table.GridColumnStyles.Add(column4);

            DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();
            column5.MappingName = "clrExaminationDateVN";
            column5.HeaderText = "Ngày Lập Bệnh Án";
            column5.Width = 140;
            column5.NullText = string.Empty;
            table.GridColumnStyles.Add(column5);

            DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();
            column6.MappingName = InfoClinicalRecord.CLREXAMINATIONTIMES;
            column6.HeaderText = "Lần Khám";
            column6.Width = 120;
            column6.NullText = string.Empty;
            table.GridColumnStyles.Add(column6);

            DataGridTextBoxColumn column7 = new DataGridTextBoxColumn();
            column7.MappingName = InfoClinicalRecord.CLRREXAMINATIONTIMES;
            column7.HeaderText = "Lần Tái Khám";
            column7.Width = 120;
            column7.NullText = string.Empty;
            table.GridColumnStyles.Add(column7);

            this.dataGrid1.TableStyles.Add(table);
            table.AllowSorting = false;
        }
    }
}